package com.tosit.bmx.controller.user;

import com.tosit.bmx.common.json.JSONModel;
import com.tosit.bmx.common.json.JSONUtil;
import com.tosit.bmx.domian.*;
import com.tosit.bmx.service.user.LevelService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 用户权限管理类
 *
 * @author tsl
 * @create 2018-04-24 9:33
 **/
@Controller
@RequestMapping()
public class LevelController {
    @Autowired
    LevelService levelService;


    /**
     * 根据用户id查找用户所能关联的所有权限（题型，科目，车型，前提题型未被禁用）
     * 和所有用户能用的权限
     * @author tsl
     * @date 2018/4/24 9:38
     * @param
     * @return
     */
    @RequestMapping(value = "/levelAndUserLe",method = RequestMethod.GET)
    public void gainLevelsByUser(Integer id, HttpServletResponse response) {
        //用户能用的权限
        List<CartypeVo> uservos= levelService.gainLevelByUser(id);
        //所有用户能用的权限
        List<CartypeVo> allvos= levelService.gainLevel();

        if (uservos != null&& allvos!=null) {
           JSONModel.put("uservos",uservos);
                   JSONModel.put("allvos",allvos);
        } else {
           JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


    /**
     * 根据用户id查找用户所能关联的所有权限（题型，科目，车型,套题，前提题型未被禁用）
     * @author tsl
     * @date 2018/4/24 9:38
     * @param
     * @return
     */
    @RequestMapping(value = "/levelAndUserLeSecondtype",method = RequestMethod.GET)
    public void gainLevelsAndSeByUser(Integer id, HttpServletResponse response) {
        //用户能用的权限
        List<CartypeVoEnhance> uservos= levelService.gainLevelsAndSeByUser(id);
        if (uservos != null) {
            JSONModel.put("uservos",uservos);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


    /**
     * 查找所有权限（题型，科目，车型）
     * @author tsl
     * @date 2018/4/24 10:38
     * @param
     * @return
     */
    @RequestMapping(value = "/level",method = RequestMethod.GET)
    public void gainAllLevel(HttpServletResponse response) {
        List<CartypeVo> uservos= levelService.gainAllLevel();
        if (uservos != null) {
           JSONModel.put("uservos",uservos);
        } else {
           JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 查找所有权限（题型，科目，车型，题型未被禁用）
     * @author tsl
     * @date 2018/4/24 10:38
     * @param
     * @return
     */
    @RequestMapping(value = "/level/a",method = RequestMethod.GET)
    public void gainALevel(HttpServletResponse response) {
        //所有用户能用的权限
        List<CartypeVo> allvos= levelService.gainLevel();
        if (allvos != null) {
            JSONModel.put("uservos",allvos);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }





    /**
     * 删除一个或多个用户关联的所有权限（题型，科目，车型）再根据用户id和题型等（id，type）
     * 再重新为他们添上权限
     * @author tsl
     * @date 2018/4/24 10:38
     * @param levels  levelVo里有用户id集合和改成权限UserLevel的集合（lid,type）
     * @return
     */
    @RequestMapping(value = "/level",method = RequestMethod.PUT)
    public void deleteLevelByUser(@RequestBody levelVo levels, HttpServletResponse response) {
        List<Integer> ids = levels.getIds();
        List<UserLevel> ss = levels.getLevels();
        List<UserLevel> resultLevel = new ArrayList<>();
        for (int i = 0; i < ids.size(); i++) {
            for (int j = 0; j < ss.size(); j++) {
                UserLevel level = ss.get(j);
                UserLevel userLevel = new UserLevel();
                userLevel.setUid(ids.get(i));
                userLevel.setLid(level.getLid());
                userLevel.setType(level.getType());
                resultLevel.add(userLevel);
            }
        }
        Integer result= levelService.deleteLevelByUser(ids);
        Integer createresult=levelService.createLevelsByUser(resultLevel);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }






    /**
     * 查找所有权限和套题（题型，科目，车型,套题）
     * @author tsl
     * @date 2018/4/24 10:38
     * @param
     * @return
     */
    @RequestMapping(value = "/levelAndSecond",method = RequestMethod.GET)
    public void gainLevelAndSecond( HttpServletResponse response) {
        //用户能用的权限
        List<CartypeVoEnhance> uservos= levelService.gainLevelAndSecond();
        if (uservos != null) {
            JSONModel.put("uservos",uservos);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


    /**
     * 用户选择界面  查找他所关联的所有权限（题型，科目，车型,套题，前提题型未被禁用）
     * @author tsl
     * @date 2018/4/24 9:38
     * @param
     * @return
     */
    @RequestMapping(value = "/user/levelAndUserLeSecondtype",method = RequestMethod.GET)
    public void gainLevelsAndSeByUser( HttpServletResponse response) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getSession().getAttribute("user");
        //用户能用的权限
        List<CartypeVoEnhance> uservos= levelService.gainLevelsAndSeByUser(user.getId());
        if (uservos != null) {
            JSONModel.put("uservos",uservos);
            JSONModel.put("user",user);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


}
